<?php 
/*****************
* 任务站点管理
*创建日期:2012-8-27
*作者：tjx
******************/
define('IN_WONDERSHARE', true);
header("Content-type:text/html;charset=utf-8");
ob_start("ob_gzhandler");
require('../includes/init.php');
require ('./configs/site_config.php');

class TaskSite {
	
	
	private  $db;
	private  $table;
	/**
	* @ 构造函数
	* @ __construct
	*/
	
	public function __construct() {

		$this->db = $GLOBALS['db'];
		$this->table = 'task_site';
	}


	/**
	* 查看站点列表
	* @param   string      		 $q_user_name      用户名称
	* @return  array
	*/
	public function index() {
		
		$site_rows = array();
		$page = 1;
		if ( !empty($_REQUEST['page']) ) {
			$page = (int)$_REQUEST['page'];
		}
		$pagesize = 10;
		
		$sql = "SELECT COUNT(*) AS total 
				FROM {$this->table}  
				WHERE  sIsDelete = 0  ";
		$page_info = get_page_info($sql, $pagesize, $page);
		$page_url = get_page_url($page, $page_info['page_num']);
		
		$sql = "SELECT 
				sId,
				sMid,
				sName, 
				sDomainName, 
				sIsCdn, 
				sCdnEmail, 
				sUpdateUser, 
				sUpdateTime
				FROM {$this->table} 
				WHERE sIsDelete = 0 
				ORDER BY sId ASC
				LIMIT {$page_info['start']}, {$pagesize}";
		$result = $this->db->query($sql);
		while ($row = $this->db->fetch_array($result, MYSQL_ASSOC)) {
			$row['sUpdateTime'] = date('Y-m-d H:i:s', $row['sUpdateTime']);
			$site_rows[] = $row;
			
		}
		

		$GLOBALS['smarty']->assign('page_url', $page_url);
		$GLOBALS['smarty']->assign('site_rows', $site_rows);
		$GLOBALS['smarty']->assign('total', $page_info['total']);
		$GLOBALS['smarty']->display('task_site_list.htm');  

		
	}
	
	/**
	* 修改或增加站点
	* @param   string      		 $q_user_name      用户名称
	* @return  array
	*/
	public function editData() {
		
		$site_rows = $select_checkbox_array  = array();
		$sId = (int) $_REQUEST['sId'];
	
		if ( !empty($sId) ) {// 编辑
			//队列处理
			queueInfo($sId);	
				
			$sql = "SELECT 
					sId,
					sMid,
					sName, 
					sDomainName, 
					sIsCdn, 
					sDirPath,
					sCdnEmail,
					sSer_Id,
					sDescription
					FROM {$this->table} 
					WHERE sId = {$sId} AND sIsDelete = 0 LIMIT 1";
			$site_row = $this->db->getOneRs($sql);
			$select_checkbox_array = explode(',', $site_row['sSer_Id']);
			
			$GLOBALS['smarty']->assign('form_act', 'edit');
			$GLOBALS['smarty']->assign('form_value', '编辑');
			
		} else {//增加
			$GLOBALS['smarty']->assign('form_act',     'add');
			$GLOBALS['smarty']->assign('form_value',     '增加');
		}
		
		$sql = "SELECT serId AS id, serName  AS name
				FROM task_server 
				WHERE serIsDelete = 0";
		$init_checkbox_array = $this->db->getAll($sql);
		$server_checkbox_str = get_checkbox($init_checkbox_array, $select_checkbox_array, 'server');
		$GLOBALS['smarty']->assign('server_checkbox_str', $server_checkbox_str);
		$GLOBALS['smarty']->assign('site_row', $site_row);
		$GLOBALS['smarty']->display('task_site_info.htm');  

		
	}
	
	/**
	* 提交操作
	* @param   array      	$data
	* @return  string
	*/
	public function editSubmit() {
		
		//验证数据合法性
		if (empty($_POST['Name']) || empty($_POST['DomainName'])) {
			
			echo '站点名称或站点域名不能为空！';
			exit;
		}
		
		//封装数据
		$sId = (int) $_POST['Id'];
		$data = array(	'sName' => trim($_POST['Name']),
						'sDomainName' => trim($_POST['DomainName']),
						'sDirPath' => trim($_POST['DirPath']),
						'sMid' => (int) $_POST['Mid'],
						'sIsCdn' => trim($_POST['IsCdn']),
						'sSer_Id' => trim($_POST['hidden_sSer_Id']),
						'sCdnEmail' => trim($_POST['CdnEmail']),
						'sDescription' => trim($_POST['Description']),
						'sUpdateTime' => $_SERVER['REQUEST_TIME'],
						'sUpdateUser' => $_SESSION['AName'],
					);
					
		//提交表单
		if( $_POST['act'] == 'add' && empty($sId) ) { //增加站点操作

			$data['sCreatTime'] = $_SERVER['REQUEST_TIME'];
			$data['sCreatUser'] = $_SESSION['AName'];
			//增加站点
			$sId = $this->db->insert($this->table, $data);
			if ( !empty($sId) ) {
				
				$location_url = "/wondershare/task_site.php";
				$alert_title = '增加成功！';
				
			} else {
				$location_url = "/wondershare/task_site.php?action=editData";
				$alert_title = '增加失败！';
			
			}
			
			mainAlert($alert_title, $location_url);
			
		} elseif ( $_POST['act'] == 'edit' && !empty($sId)) {//修改站点操作
			//删除队列
			queueDelete(0, $sId);
			//返回影响的行数
			$lines = $this->db->update($this->table, $data, " sId = {$sId} AND sIsDelete = 0  ");
			if ( $lines > 0 ) {
				
				$location_url = "/wondershare/task_site.php";
				$alert_title = '修改成功！';
				
			} else {
				$location_url = "/wondershare/task_site.php?action=editData&sId={$sId}";
				$alert_title = '修改失败！';
			
			}
			mainAlert($alert_title, $location_url);
			
		}
	}
	
	/**
	* 验证数据
	* @param   array      	$data
	* @return  string
	*/
	public function checkDate($data) {
		
		if (empty($data['sName']) || empty($data['sDomainName'])) {
			
			echo '站点名称或站点域名不能为空！';
			exit;
		}
	}
	
	/**
	* 删除队列记录
	* @param   int      		$qid       队列主键
	* @param   int        	 	 $id       编辑ID
	* @return  array
	*/
	public function delete() {
		
		$str_site_id = trim($_GET['str_site_id']);
		if ( !empty($str_site_id) ) {
			
			$data = array(	'sUpdateTime' => $_SERVER['REQUEST_TIME'],
							'sUpdateUser' => $_SESSION['AName'],
							'sIsDelete' => 1
						);
						
			$where_str = " FIND_IN_SET(sId, '{$str_site_id}') ";
			$rows_affected = $this->db->update($this->table, $data, $where_str);	
			$str = $rows_affected >= 1 ? '删除成功' : '删除失败'; 
			echo $str;
		}
		
		
	}


}

$action    = $_REQUEST['action']; 
$task_site   = new TaskSite;
if ( method_exists($task_site, $action) )  {
	$task_site->$action();
} else {
	$task_site->index();
}

?>
